package adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.qianfeng.lingshijia.R;
import com.qianfeng.lingshijia.SchoolSelectActivity;

import java.util.List;

import entity.SchoolListEntity;

/**
 * 大学选择的适配器
 */
public class SchoolSelectAdapter extends BaseAdapter implements View.OnClickListener{

    private List<SchoolListEntity> list;
    private SchoolSelectActivity ssActity;

    public SchoolSelectAdapter(List<SchoolListEntity> list, SchoolSelectActivity ssActity) {
        this.list = list;
        this.ssActity = ssActity;
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int type = list.get(position).getType();
        //如果contentView不为空，或者他的类型和新的类型一致，更换TextView的text
        if (convertView != null && ((ViewHolder) convertView.getTag()).type == type) {
            ((ViewHolder) convertView.getTag()).textView.setText(list.get(position).getName());
        } else switch (type) {
            case 0://类型为0是大学，加载布局，添加标签，设置显示的text
                convertView = LayoutInflater.from(ssActity).inflate(R.layout.school_name_layout, null);
                ViewHolder holder1 = new ViewHolder();
                holder1.type = 0;
                holder1.textView = (TextView) convertView.findViewById(R.id.tvSchoolName);
                holder1.textView.setText(list.get(position).getName());
                holder1.textView.setOnClickListener(this);
                convertView.setTag(holder1);
                break;
            case 1://类型为1是校区，加载布局，添加标签，设置显示的text
                convertView = LayoutInflater.from(ssActity).inflate(R.layout.school_area_layout, null);
                ViewHolder holder2 = new ViewHolder();
                holder2.type = 1;
                holder2.textView = (TextView) convertView.findViewById(R.id.tvSchoolAreaName);
                holder2.textView.setText(list.get(position).getName());
                convertView.setTag(holder2);
                break;
        }
        return convertView;
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.tvSchoolName){
            int schoolId = -1;//记录学校ID
            String schoolName = ((TextView) v).getText().toString();//获取点击学校的名字
            //找到点击学校的ID
            for(int i =0;i<list.size();i++){
                if(list.get(i).getName().equals(schoolName)){
                    schoolId = list.get(i).getId();
                    break;
                }
            }
            //如果找到，传参
            if(schoolId!=-1){
                Intent intent = ssActity.getIntent().putExtra("schoolName",schoolName);
                ssActity.setResult(schoolId, intent);
            }

            ssActity.finish();
        }
    }
}

/**
 * 携带数据的标签，绑定在contentView的标签，type表示当前的类型，TextView是显示的文本
 */
class ViewHolder {
    int type;
    TextView textView;
}
